<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bus:extd:yfYlfw:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusExtdYfYlfwForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { YfYlfw, yfYlfwSave, yfYlfwForm } from '/@/api/bus/extd/yfYlfw';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bus.extd.yfYlfw');
  const { showMessage } = useMessage();
  const record = ref<YfYlfw>({} as YfYlfw);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增医疗废物') : t('编辑医疗废物'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('_locked'),
      field: 'locked',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
    {
      label: t('_sortkey'),
      field: 'sortkey',
      component: 'Input',
      componentProps: {
        maxlength: 26,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('日期'),
      field: 'ddate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        showTime: false,
      },
    },
    {
      label: t('线路'),
      field: 'croute',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('医院'),
      field: 'hospName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('感染性'),
      field: 'weightGgr',
      component: 'Input',
      componentProps: {
        maxlength: 51,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('损伤性'),
      field: 'weightPs',
      component: 'Input',
      componentProps: {
        maxlength: 51,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('总量'),
      field: 'weightSum',
      component: 'Input',
      componentProps: {
        maxlength: 51,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('区域'),
      field: 'area',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('感染性箱数'),
      field: 'qtyGr',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('损伤性箱数'),
      field: 'qtyPs',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('总箱数'),
      field: 'qtySum',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('线路顺序'),
      field: 'routeSort',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('医疗机构性质'),
      field: 'ctype',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('收运车辆'),
      field: 'carNo',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('驾驶员'),
      field: 'driver',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('押运员'),
      field: 'driver2',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
    },
    {
      label: t('收运时间'),
      field: 'shDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('接收时间'),
      field: 'jsDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('处置时间'),
      field: 'dealDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('联单编号'),
      field: 'extDjno',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('存档'),
      field: 'bsp',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await yfYlfwForm(data);
    record.value = (res.yfYlfw || {}) as YfYlfw;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await yfYlfwSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
